//
//  MYDanViewController.m
//  彩票
//
//  Created by 冀柳冲 on 2017/8/15.
//  Copyright © 2017年 冀柳冲. All rights reserved.
//

#import "MYDanViewController.h"
#import "UserInfo.h"
#import "UserCell.h"
#import "DanZi.h"
#import "DanZiCell.h"
#import "DetailViewController.h"

@interface MYDanViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong) UITableView * tableView;
@property(nonatomic,strong) NSMutableArray * dataSource;
@end

@implementation MYDanViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.navigationItem.title = @"计算...";
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self loadData];
}
- (void)loadData{
    self.dataSource = [NSMutableArray array];
    [QMUITips showLoadingInView:[UIApplication sharedApplication].keyWindow];
    [NewNetWorkManager requestGETWithURLString:[NSString stringWithFormat:@"http://mapi.91alli.com/service.json?authedUserId=%@&service=PURCHASE_ORDER_QUERY_BY_PURCHASE_NO&purchaseNo=%@&queryEntries=1&loginKey=LOGIN_20171205220734",self.userid,self.purchaseNo] parDic:nil finish:^(NSDictionary * respon) {
        
        DetailViewController *VC = [DetailViewController new];
        VC.response = respon;
        [self.navigationController pushViewController:VC animated:YES];
        
        [QMUITips hideAllToastInView:[UIApplication sharedApplication].keyWindow animated:YES];

        
    } conError:^(NSError *error) {
        [QMUITips hideAllToastInView:[UIApplication sharedApplication].keyWindow animated:YES];
        [QMUITips showError:@"网络错误" inView:[UIApplication sharedApplication].keyWindow hideAfterDelay:2];

    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark--UITableViewDelegate

//row height
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 70;
}
//header height
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01;
}
//footer height
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}
// view--header
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
// view-footer
- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    return [UIView new];
}
#pragma mark--UITableViewDataSource
//row 个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}

//section 个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
//自定义cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DanZiCell *cell = [tableView dequeueReusableCellWithIdentifier:@"danzicell"];
    cell.model = self.dataSource[indexPath.row];
    return cell;
}

#pragma mark - 点击跳转方法

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    DanZi *model = self.dataSource[indexPath.row];
    DetailViewController *VC = [DetailViewController new];
    
    //authedUserId=8201706251629517&loginKey=JCZJ_LOGIN20171031202614&purchaseNo=98012017110947954261&queryEntries=1&service=LOTTERY_PURCHASE_DETAIL_QUERY_BY_PURCHASE_NO&sign=b016f6d996ad80ef9a3e5fd62db9d2ab
    
    [NewNetWorkManager requestPOSTWithURLString:[NSString stringWithFormat:@"http://api.jczj123.com/client/service.json?authedUserId=%@&loginKey=JCZJ_LOGIN20171031202614&purchaseNo=%@&queryEntries=1&service=LOTTERY_PURCHASE_DETAIL_QUERY_BY_PURCHASE_NO&&sign=0a93c49ae380ce334e924c1d66c91418",model.ownerUserId,model.purchaseNo] parDic:nil finish:^(id responseObject) {
        NSMutableArray *array = [NSMutableArray array];
        NSDictionary *meths = responseObject[@"response"][@"purchaseOrder"];
        NSString *str = [NSMutableString string];
        for (NSDictionary *dic  in meths[@"mixTypes"]) {
            str =  [str stringByAppendingString:dic[@"message"]];
            str =  [str stringByAppendingString:@" - "];
        }
        VC.m = str;
        VC.response = responseObject;
        
        for (NSString *key in [responseObject[@"response"][@"entryQueryResponse"][@"spMap"] allKeys]) {
            NSArray *value = responseObject[@"response"][@"entryQueryResponse"][@"spMap"][key];
            NSString *val = key;
            if ([val hasSuffix:@"DRAW"]) {
                if ([val containsString:@"RQSPF"]) {
                    val = [val stringByAppendingString:@"-让球平"];
                }else{
                    val = [val stringByAppendingString:@"-平"];
                }
            }
            
            if ([val hasSuffix:@"LOST"]) {
                if ([val containsString:@"RQSPF"]) {
                    val = [val stringByAppendingString:@"-让球输"];
                }else{
                    val = [val stringByAppendingString:@"-输"];
                }
            }
            if ([val hasSuffix:@"WIN"]) {
                if ([val containsString:@"RQSPF"]) {
                    val = [val stringByAppendingString:@"-让球赢"];
                }else{
                    val = [val stringByAppendingString:@"-赢"];
                }
            }
            
            
            NSLog(@"%@",value);
            [array addObject:[NSString stringWithFormat:@"%@ - %@",val,value.lastObject]];
            
        }
        VC.dataSource = array;
        [self.navigationController pushViewController:VC animated:YES];
        
    } conError:^(NSError *error) {
        
    }];
 
    
}

#pragma mark - lazy load

-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
        
        _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        _tableView.separatorColor = [UIColor lightGrayColor];
        _tableView.separatorInset = UIEdgeInsetsMake(0, 13, 0, 0);
        _tableView.tableFooterView = [UIView new];
        _tableView.estimatedRowHeight = 70;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([DanZiCell class]) bundle:nil] forCellReuseIdentifier:@"danzicell"];
        [self.view addSubview:_tableView];
    }
    return _tableView;
}


@end
